<template>
  <div class="commonHeader">
    <div class="left">
      <img
        src="@/assets/image/logo.png"
        class="logo"
        alt=""
        style="margin-right: 20px"
      />
    </div>
    <div class="right">
      <div class="weather">
        <p class="dfac">
          {{ weather.city }}
          <el-image
            v-if="weather.wea_img"
            :src="require(`@/assets/image/weather/${weather.wea_img}.png`)"
            style="margin: 0 10px; width: 30px"
          ></el-image>
          {{ weather.win }} {{ weather.tem2 }}℃ ~{{ weather.tem1 }}℃
          <span
            class="air_level"
            :style="{
              background: getWeatherColor(weather.air_level),
            }"
            >{{ weather.air_level }}</span
          >
        </p>
        <p>{{ date }}</p>
      </div>
      <div
        style="
          height: 30px;
          margin: 0 20px;
          border-right: 2px solid rgba(255, 255, 255, 0.5);
        "
      ></div>
      <div class="userMsg">
        <el-image
          :src="
            '/credit/manage/common/file/download?id=' +
            $store.state.user.userInfo.headImgUrl
          "
          style="
            width: 50px;
            height: 50px;
            margin-right: 10px;
            border-radius: 100%;
          "
        >
          <div slot="error" class="image-slot">
            <img
              src="@/assets/image/user_logo.png"
              alt=""
              style="width: 50px; height: 50px; margin-right: 10px"
            />
          </div>
        </el-image>
        <span>{{
          $store.state.user.userInfo.name ||
          $store.state.user.userInfo.nickName ||
          $store.state.user.userInfo.account
        }}</span>
      </div>
      <div
        style="
          height: 30px;
          margin: 0 20px;
          border-right: 2px solid rgba(255, 255, 255, 0.5);
        "
      ></div>
      <div class="dfac cp" @click="handelOut">
        <img src="@/assets/image/out.png" style="margin-right: 5px" alt="" />
        <span>退出</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'commonHeader',
  data() {
    return {
      date: '',
      timer: null,
      weather: {},
      job: '',
      companyMsg: {}, //当前选择企业
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  mounted() {
    this.getWeather()
  },
  methods: {
    //得到天气
    async getWeather() {
      let res = await this.$http.get(
        'https://tianqiapi.com/api?version=v6&appid=66527314&appsecret=79zfaynW'
      )
      this.weather = res
    },
    //处理天气颜色
    getWeatherColor(name) {
      let color = '#17D687'
      if (name === '优') {
        color = '#17D687'
      } else if (name === '良') {
        color = '#FBCF1E'
      } else if (name === '轻度污染') {
        color = '#FBA01E'
      } else if (name === '中度污染') {
        color = '#FB1E1E'
      } else if (name === '重度污染') {
        color = '#AB0044'
      } else if (name === '严重污染') {
        color = '#AB0044'
      }
      return color
    },
    //退出登录
    handelOut() {
      this.$store.commit('clearCookie')
      this.$router.replace({
        path: '/',
      })
    },
  },
}
</script>

<style scoped lang="scss">
.commonHeader {
  height: 100px;
  background: #0074de;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px 0 40px;
  .logo {
    width: 50px;
    height: 50px;
    border-radius: 100%;
    overflow: hidden;
  }
  .right {
    font-size: 14px;
    font-weight: 400;
    color: #fefefe;
    display: flex;
    align-items: center;
    .weather {
      line-height: 24px;
    }
    .userMsg {
      display: flex;
      align-items: center;
      color: #fff;
    }
  }
  .air_level {
    padding: 4px;
    border-radius: 2px;
    color: #fefefe;
    text-align: center;
    line-height: 20px;
    margin-left: 10px;
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #fff;
    font-size: 16px;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
}
</style>
